home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / comavail.zip / COMAVAIL.C next >
C/C++ Source or Header  |  1992-11-15  |  920b  |  41 lines

  1. /*
  2.  * COMAVAIL --- COM port availability
  3.  */
  4.  
  5. #include <process.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <fcntl.h>
  9.  
  10. int
  11. main(int argc, char **argv)
  12. {
  13.     char path[64];
  14.     int port, rc;
  15.  
  16.     if (argc < 3)
  17.     {
  18.     fprintf(stderr, "usage: comavail portnumber command [args]\n");
  19.     return 255;
  20.     }
  21.     if ((port = atoi(argv[1])) < 1 || port > 8)
  22.     {
  23.     fprintf(stderr, "comavail: port number must be between 1 and 8\n");
  24.     return 255;
  25.     }
  26.     strcpy(path, getenv("TEMP"));
  27.     strcat(path, "\\LOCK_");
  28.     strcat(path, argv[1]);
  29.     strcat(path, ".LCK");
  30.     if ((port = open(path, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1)
  31.     {
  32.     perror("comavail: can't lock");
  33.     return 255;
  34.     }
  35.     close(port);
  36.     if ((rc = spawnvp(P_WAIT, argv[2], argv + 2)) == -1)
  37.     perror("comavail: can't run program");
  38.     if (unlink(path) != 0)
  39.     perror("comavail: can't unlock");
  40.     return rc;
  41. }